//
//  WXImageCell.m
//  Component
//
//  Created by Andy_chen on 16/4/8.
//  Copyright © 2016年 Andycchen. All rights reserved.
//

#import "WXImageCell.h"

@implementation WXImageCell

- (void)setAsset:(WXAsset *)asset {
    _asset = asset;
    _imageView.image = [UIImage imageWithCGImage:asset.asset.thumbnail];
    [self changeButtonImage:asset.selected];
}

- (IBAction)selectAction:(id)sender {
    
    [self changeButtonImage:!self.asset.selected];
    
}

- (void)changeButtonImage:(BOOL)selected {
    
    if (_delegate && [_delegate respondsToSelector:@selector(shouldSelectAsset:previousCount:)] && selected) {
        
        self.asset.selected = [_delegate shouldSelectAsset:self.asset previousCount:self.asset.index];
        
        if (!self.asset.selected) return;
        
        [self showSelectButtonAnimation];
        
    }
    else if (_delegate && [_delegate respondsToSelector:@selector(shouldDeselectAsset:previousCount:)] && !selected) {
        
        [_delegate shouldDeselectAsset:self.asset previousCount:self.asset.index];
        self.asset.selected = NO;
    }
    
    UIImage *image = selected?[UIImage imageNamed:@"FriendsSendsPicturesSelectYIcon"]:[UIImage imageNamed:@"FriendsSendsPicturesSelectIcon"];
    
    [_selectButton setImage:image forState:UIControlStateNormal];
    
}


- (void)showSelectButtonAnimation {
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    animation.duration = 0.2;
    animation.repeatCount = 1;
    animation.autoreverses = YES;
    
    animation.fromValue = [NSNumber numberWithFloat:1.2];
    animation.toValue = [NSNumber numberWithFloat:1.0];
    
    [_selectButton.layer addAnimation:animation forKey:@"scale-layer"];
}

@end
